package com.hrbxlh.util;
import java.io.OutputStream;  
import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;  

import javax.servlet.http.HttpServletRequest;  
import javax.servlet.http.HttpServletResponse;  

import org.apache.poi.hssf.usermodel.HSSFCell;  
import org.apache.poi.hssf.usermodel.HSSFCellStyle;  
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;  
import org.apache.poi.hssf.usermodel.HSSFSheet;  
import org.apache.poi.hssf.usermodel.HSSFWorkbook;  
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.servlet.view.document.AbstractExcelView;  

public class SourceExcel extends AbstractExcelView {  
		private XSSFWorkbook wb = null;  
	  
	    private XSSFSheet sheet = null; 
	  @Override
    protected void buildExcelDocument(Map<String, Object> model, HSSFWorkbook workbook, HttpServletRequest request, HttpServletResponse response) throws Exception {
 
        List<Map<String,Object>> memberList = (List<Map<String,Object>>) model.get("memberList");
        String[] keys = (String[])model.get("keys");
        String[] titles = (String[])model.get("titles");
        String fileName = (String)model.get("fileName");
        HSSFSheet sheet = workbook.createSheet();
        sheet.setDefaultColumnWidth(12);
      
        //调用共有的类，调用共有的方法
        HSSFCellStyle style = PubMethods.cssStyle(workbook);
        
        HSSFCellStyle style1 = workbook.createCellStyle();
        HSSFFont font1 = workbook.createFont();
        font1.setFontHeight((short)220);  
        font1.setFontName("宋体"); 
        style1.setFont(font1);
//        style.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);

        for (int i = 0; i < titles.length; i++) {
            HSSFCell cell = getCell(sheet,0,i);
            if(i==2||i==3||i==4){
            	sheet.setColumnWidth(i, 7000);
            }
            cell.setCellStyle(style);
            setText(cell, titles[i]);
        }
 
        for (int i = 0; i < memberList.size(); i++) {
            HSSFRow row = sheet.createRow(i+1);
            row.setRowStyle(style1);
            Map<String,Object> map = memberList.get(i);
            // 处理列
            for(int j=0; j < keys.length; j++) {
            	
            	 HSSFCell contentCell = row.createCell(j);
            	 contentCell.setCellStyle(style1);
            	 String value = PubMethods.getFieldValueByName(keys[j], map);
            	 System.out.println("成绩"+value);
            	 if(keys[j].equals("examSource")){
            		 if(value==null||value.equals("null")||value.equals("")||value.equals("0.00")||value.equals("0")){
            			//考试成绩默认是没有的是空的一列，需要老师进行填写
                		 value="";
            		 }
            		 
            	 }
            	 contentCell.setCellValue(value);
               
            }
        }

        PubMethods.excelOut(fileName, response, workbook);
 
    }
  
}  